<?php
/**
 * Helper for setting and retrieving javascripts in conjunction with the js
 * minifier library.
 *
 * @package    Clients
 * @subpackage View Helpers
 * @copyright  (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @author     Luke Visinoni <luke.visinoni@gmail.com>
 * @version    $Id$
 */
class Visi_View_Helper_HeadScriptMin extends Zend_View_Helper_HeadScript {

    /**
     * A list of javascripts to minify
     */
    protected $_minifiedJavascripts = array();
    
    /**
     * @var string registry key
     */
    protected $_regKey = 'Visi_View_Helper_HeadScriptMin';
    
    /**
     * Append javascript(s) using the js minify library
     */
    public function appendMinifiedFile($name, Array $srcs, $type='text/javascript', $attrs = array()) {
    
        if (array_key_exists($name, $this->_minifiedJavascripts)) {
            if (array_key_exists('srcs', $this->_minifiedJavascripts[$name])) {
                $srcs = array_merge($this->_minifiedJavascripts[$name]['srcs'], $srcs);
            }
        }
        $this->_minifiedJavascripts[$name] = array(
            'srcs' => $srcs,
            'type' => $type,
            'attrs' => $attrs,
        );
        return $this;
    
    }
    
    /**
     * Adds a file to the specified minified javascript
     * @todo Maybe the first part of this (the array key exists part) should use $this->appendMinifiedFile()
     */
    public function addFile($name, $file) {
    
        if (!array_key_exists($name, $this->_minifiedJavascripts)) {
            $this->_minifiedJavascripts[$name] = array(
                'srcs' => array(),
                'type' => 'text/javascript',
                'attrs' => array(),
            );
        }
        $this->_minifiedJavascripts[$name]['srcs'][] = $file;
        return $this;
    
    }
    
    /**
     * Output the minified head script
     */
    public function headScriptMin($mode = Zend_View_Helper_HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript') {
    
        return $this->headScript($mode, $spec, $placement, $attrs, $type);
    
    }
    
    /**
     * Render link elements as string
     *
     * @param  string|int $indent
     * @return string
     * @todo   See if it is possible to call parent::toString() rather than recreating the whole method
     */
    public function toString($indent = null) {
    
        $indent = (null !== $indent)
                ? $this->getWhitespace($indent)
                : $this->getIndent();
        if ($this->view) {
            $useCdata = $this->view->doctype()->isXhtml() ? true : false;
        } else {
            $useCdata = $this->useCdata ? true : false;
        }
        $escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
        $escapeEnd   = ($useCdata) ? '//]]>'       : '//-->';
        foreach ($this->_minifiedJavascripts as $name => $javascript) {
            $files = implode(',', array_unique($javascript['srcs']));
            $src = '/min/f=' . $files;
            $this->appendFile($src, $javascript['type'], $javascript['attrs']);
        }
        $items = array();
        $this->getContainer()->ksort();
        foreach ($this as $item) {
            if (!$this->_isValid($item)) {
                continue;
            }
            $items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
        }
        $return = implode($this->getSeparator(), $items);
        return $return;
    
    }

}
